package com.example.blogdesign.Service.Impl;

import cn.hutool.core.bean.BeanUtil;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.blogdesign.Entity.Follow;
import com.example.blogdesign.Mapper.FollowMapper;
import com.example.blogdesign.Service.IFollowService;
import com.example.blogdesign.Service.IUserInfoService;
import com.example.blogdesign.Service.IUserService;
import com.example.blogdesign.dto.Result;
import com.example.blogdesign.dto.UserDTO;
import com.example.blogdesign.utils.UserHolder;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.example.blogdesign.utils.RedisConstants.FOLLOW_KEY;


@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private IUserService userService;

    @Resource
    private IUserInfoService userInfoService;

    //关注或者取关
    @Transactional
    @Override
    public Result<?> followOrNot(Long followUserId, Boolean isFollow) {
        Long userId = UserHolder.getUser().getId();
        String key=FOLLOW_KEY+userId;
        if (isFollow){
            //关注
            Follow follow=new Follow();
            follow.setUserId(userId);
            follow.setFollowUserId(followUserId);
            //保存关注信息
            save(follow);
            userInfoService.modifyFansNumber(followUserId, true);
            userInfoService.modifyFollowNumber(userId,true);
            stringRedisTemplate.opsForSet().add(key,followUserId.toString());

        }
        else {
            //取关
            remove(new QueryWrapper<Follow>()
                    .eq("user_id", userId)
                    .eq("follow_user_id", followUserId));
            userInfoService.modifyFansNumber(followUserId,false);
            userInfoService.modifyFollowNumber(userId,false);
            stringRedisTemplate.opsForSet().remove(key,followUserId.toString());
        }
        return Result.ok();
    }

    //判断是否关注
    @Override
    public Result<Boolean> isFollow(Long followUserId) {
        Long userId = UserHolder.getUser().getId();
        //查询是否关注
        Long count = query().eq("user_id", userId).eq("follow_user_id", followUserId).count();
        return Result.ok(count>0);
    }

    //查询共同关注
    @Override
    public Result<List<UserDTO>> commonFollow(Long id) {
        //获取当前用户
        Long userId = UserHolder.getUser().getId();
        String key1=FOLLOW_KEY+userId;
        //求两个用户共同关注，求交集。
        String key2=FOLLOW_KEY+id;
        Set<String> intersect = stringRedisTemplate.opsForSet().intersect(key1, key2);
        if (intersect==null||intersect.size()==0){
            return Result.ok();
        }
        //共同关注用户id列表
        List<Long> ids = intersect.stream().map(Long::valueOf).collect(Collectors.toList());
        //根据用户id查出用户列表
        List<UserDTO> userDTOList = userService.listByIds(ids).stream()
                .map(user -> BeanUtil.copyProperties(user, UserDTO.class))
                .collect(Collectors.toList());
        System.out.println(userDTOList);
        return Result.ok(userDTOList);
    }

}
